package com.da.boot.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import com.da.boot.bean.Student;
import com.da.boot.bean.User;
import com.da.boot.service.StudentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author JokerDa.
 * @date 2021/1/22 15:26
 * Description:
 */
@Slf4j
@Controller
public class IndexController {

    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Resource
    private StudentService studentService;
    @Autowired
    private StringRedisTemplate redisTemplate;

    @ResponseBody
    @GetMapping("/sql")
    public String queryFromDb() {
        Long aLong = jdbcTemplate.queryForObject("select count(*) from student", Long.class);
        return aLong.toString();
    }

    @ResponseBody
    @GetMapping("/stu")
    public Student getById(@RequestParam("id") int id) {
        return studentService.getStudent(id);
    }

    @ResponseBody
    @GetMapping("/all")
    public List<Student> getById() {
        return studentService.getAllStudent();
    }

    /**
     * 来登录页
     */
    @GetMapping(value = {"/", "login"})
    public String login() {
        return "login";
    }

    @PostMapping(value = {"login"})
    public String main(User user, HttpSession session, Model model) {
        if (user != null && StringUtils.hasLength(user.getUserName()) && "123456".equals(user.getPassword())) {
            //把登陆成功的用户保存起来
            session.setAttribute("loginUser", user);
            //登录成功重定向到main.html;  重定向防止表单重复提交
            return "redirect:/main.html";
        } else {
            model.addAttribute("msg", "账号密码错误");
            //回到登录页面
            return "login";
        }
    }

    /**
     * 去main页面
     */
    @GetMapping("/main.html")
    public String mainPage(HttpSession session, Model model) {

        //log.info("当前方法是：{}","mainPage");
        //是否登录。  拦截器，过滤器
        //Object loginUser = session.getAttribute("loginUser");
        //if (loginUser != null) {
        //    return "main";
        //} else {
        //    //回到登录页面
        //    model.addAttribute("msg", "请重新登录");
        //    return "login";
        //}

        ValueOperations<String, String> opsForValue = redisTemplate.opsForValue();

        String s = opsForValue.get("/main.html");
        String s1 = opsForValue.get("/sql");

        model.addAttribute("mainCount", s);
        model.addAttribute("sqlCount", s1);
        return "main";
    }
}
